/*
 * @Author: spark
 * @Date: 2021-06-21 14:11:41
 * @LastEditTime: 2021-06-29 09:41:05
 * @LastEditors: spark
 * @Description: 一个持续与bug做抗争的程序员
 * email: spark.xiaoyu@qq.com
 */

import { ActionTypes } from './action-types'
import { UserMutationsTypes } from './mutations-types'
import type { Mutations } from './mutations';

const sleep = (t: number) => new Promise(resolve => setTimeout(resolve, t))



type AugmentedActionContext = {
    commit<K extends keyof Mutations>(
        key: K,
        payload?: Parameters<Mutations[K]>[1],
    ): ReturnType<Mutations[K]>
}
export type Actions = {
    [ActionTypes.ACTION_SET_TOKEN]({ commit }: AugmentedActionContext, token: string): void
    [ActionTypes.ACTION_LOGIN_OUT]({ commit }: AugmentedActionContext): void
}
export const actions: Actions = {
    async [ActionTypes.ACTION_SET_TOKEN]({ commit }) {
        await sleep(2000)
        /**
         * @description: Request and input your token, 'mockToken' is mock data
         */        
        commit(UserMutationsTypes.MUTATIONS_SET_TOKEN, 'mockToken')
    },
    [ActionTypes.ACTION_LOGIN_OUT]({ commit }) {
        commit(UserMutationsTypes.MUTATIONS_REMOVE_TOKEN)
    }
}